import axios from "axios";
import qs from "qs";
import {getUserInfo,getToken} from "../utils/auth";
import {error} from "../utils/message";

function getUserId() {
    return getUserInfo().idAdmin;
}

/**
 * 获取系统验证码
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getValidateImg() {
    return axios.get("/public/images.php?action=captcha");
}

/**
 * 用户登录
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export function login(data) {
    return axios.post("/admin/admin.php?action=login", qs.stringify(data)
    );
}

/**
 * 退出登录
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export function logout(data) {
    return axios.post("/admin/admin.php?action=login_out&token="+getToken(), data);
}

/**
 * 一次性加载用户权限菜单
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export function permissionMenus(data) {
    let userId = data.userId || getUserId();
    return axios.post(`/admin/admin.php?action=permission&userId=${userId}&token=${getToken()}`);
}

export function permissionResources(data) {
    let userId = data.userId || getUserId();
    return axios.post(`/admin/admin.php?action=permission&userId=${userId}&menuId=${data.menuId}&token=${getToken()}`)
}

/**
 * 下载文件,
 * @param url 路径
 * @param param 参数
 */
export function download(url, param) {
    return new Promise((resolve, reject) => {
        axios.post(url+"&token="+getToken(), qs.stringify(param), {responseType: "blob"}).then(res => {
            let blob = new Blob([res.data]);
            let delement = document.createElement("a");
            let href = window.URL.createObjectURL(blob);
            delement.href = href;
            let name = decodeURI(res.headers["content-disposition"].split("=")[1]);
            delement.download = name;
            document.body.appendChild(delement);
            delement.click();
            document.body.removeChild(delement);
            window.URL.revokeObjectURL(href);
            resolve();
        }).catch(err => {
            reject(err);
        });
    });
}

/**
 * 加载已配置的菜单
 * @param param
 * @returns {Promise<AxiosResponse<*>>}
 */
export function loadConfigedMenus(param) {
    // return postRequest("/system-config/system/menusinfoEntity/getAllByCondition&token="+getToken(), param);

}

/**
 * 获取部门信息
 * @param param
 * @returns {Promise<AxiosResponse<*>>}
 */
export function loadDepartments(param) {
    // return new Promise((resolve, reject) => {
    //     postRequest("/system-config/system/departmentEntity/getAllByCondition&token="+getToken(), param).then(res => {
    //         if (res.data.code != 0) {
    //             reject(res.data.cnMessage);
    //         } else {
    //             let resdata = res.data.data;
    //             resolve(resdata)
    //         }
    //
    //     }).catch(err => reject(err))
    // })

}

/**
 * 获取下拉数据
 * @param param
 * @returns {Promise<unknown>}
 */
export function loadSelectDatas(param) {
    // return new Promise((resolve, reject) => {
    //     postRequest("/system-config/system/statusinfoEntity/getAllByCondition&token="+getToken(), param).then(res => {
    //         if (res.data.code != 0) {
    //             reject(res.data.cnMessage);
    //         } else {
    //             let resdata = res.data.data;
    //             resolve(resdata)
    //         }
    //
    //     }).catch(err => reject(err))
    // })
}

/**
 * 获取下拉数据
 * @param param
 * @returns {Promise<unknown>}
 */
export function loadRoleDatas(param) {
    // return new Promise((resolve, reject) => {
    //     postRequest("/system-config/system/rolesinfoEntity/getAllByCondition&token="+getToken(), param).then(res => {
    //         if (res.data.code != 0) {
    //             reject(res.data.cnMessage);
    //         } else {
    //             let resdata = res.data.data;
    //             resolve(resdata)
    //         }
    //
    //     }).catch(err => reject(err))
    // })
}

/**
 * Post 请求
 * @param url
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export function postRequest(url, data) {
    let sData = qs.stringify(data);
    return axios.post(url+"&token="+getToken(), sData);
}

/**
 * 通过FormData传数据
 * @param url
 * @param data
 * @returns {Promise<AxiosResponse<any>>}
 */
export async function uploadWithImage(url, data) {
    return await axios.post(url+"&token="+getToken(), data);
}

/**
 * Get请求
 * @param url
 * @returns {Promise<AxiosResponse<any>>}
 */
export function getRequest(url) {
    return axios.get(url+"&token="+getToken());

}

/**
 * 获取状态数据
 * @param name
 * @returns {Promise<{}>}
 */
export async function loadStatus(name) {
    let data = {};
    await postRequest("/admin/admin.php?action=status&token="+getToken(), {
        "statusType": name
    }).then(res => {
        data = res.data.data;
    }).catch(err => {
        error(err);
    });
    return data;
}

/**
 * 获取分类数据
 * @returns {Promise<{}>}
 */
export async function menusList() {
    let redata = {};
    await postRequest("/admin/menusinfo.php?action=list", {"isDel": 0}).then(res => {
        redata = res.data.data;
    }).catch(err => {
        error(err);
    });
    return redata;
}